package levelrewind.android.app.adapter;

import com.facebook.Session;

import levelrewind.android.app.model.PictureHandler;
import levelrewind.android.app.model.Util;
import levelrewind.android.app.model.Util.Emotion;
import levelrewind.android.app.vo.Person;
import levelrewind.android.app.MainActivity;
import levelrewind.android.app.R;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;

public class PersonListAdapter extends BaseExpandableListAdapter {

	private Context myContext;   
	private Person[] persons;
	
	
	public PersonListAdapter(Context context, Person[] persons) {    
		myContext = context; 
		this.persons = persons;
	}
	
	public void setGroup(int groupPosition, Person person) {
		persons[groupPosition] = person;
		notifyDataSetChanged();
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return persons[groupPosition];
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return groupPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {         
		Log.i("getChildView at pos", groupPosition+""); 
		if (convertView == null) {  
			 LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);     
			 convertView = inflater.inflate(R.layout.child_row, null);    
		 }
		Button btnLatest = (Button)convertView.findViewById(R.id.btnLatestScores);; 
		Button btnHigh = (Button)convertView.findViewById(R.id.btnHighScores);
		Button btnFriends = (Button)convertView.findViewById(R.id.btnFriendScores);
		Button btnPlayMain = (Button)convertView.findViewById(R.id.btnPlayMain);
		Button btnPlay = (Button)convertView.findViewById(R.id.btnPlay);
		
		btnLatest.setTypeface(Util.customFont(myContext));
		btnHigh.setTypeface(Util.customFont(myContext));
		btnFriends.setTypeface(Util.customFont(myContext));
		btnPlayMain.setTypeface(Util.customFont(myContext));
		btnPlay.setTypeface(Util.customFont(myContext));
		
		MainActivity ac = (MainActivity)myContext;
		if (ac.playCtrl == ac.GAME_LIST) {
			btnPlay.setVisibility(View.VISIBLE);
    		btnPlayMain.setVisibility(View.GONE);
		} else {
			btnPlay.setVisibility(View.GONE);
    		btnPlayMain.setVisibility(View.VISIBLE);
		}
		
		if (Util.isOnline(myContext)) 
    	{
			LinearLayout chartView = (LinearLayout)convertView.findViewById(R.id.chart);
			ListView scoreListView = (ListView)convertView.findViewById(R.id.statsScores);
			chartView.removeAllViews();
			scoreListView.setAdapter(null);
			
			btnLatest.setVisibility(View.VISIBLE);
			btnHigh.setVisibility(View.VISIBLE);
			btnFriends.setVisibility(View.VISIBLE);
			
			//Download data for latestPlayer and scores to charts
			ac.new GetLatestPlayerTask().execute(persons[groupPosition].getId());
			ac.new GetScoresTask().execute(persons[groupPosition].getId());
			
			//----Control displaying facebook's friends-----
			Session session = Session.getActiveSession();
			
			// if user is logged in with facebook
	    	if (session.isOpened()) {
	    		btnFriends.setVisibility(View.VISIBLE);
	    		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.33f); 
	    		btnLatest.setLayoutParams(params);
	    		btnHigh.setLayoutParams(params);
	    		btnFriends.setLayoutParams(params);
	    		
	    		btnLatest.setSingleLine();
	    		btnHigh.setSingleLine();
	    		btnFriends.setSingleLine();
	    	}
	    	else {
	    		btnFriends.setVisibility(View.GONE);
	    		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.65f); 
	    		btnLatest.setLayoutParams(params);
	    		params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.35f); 
	    		btnHigh.setLayoutParams(params);
	    		
	    		btnLatest.setSingleLine();
	    		btnHigh.setSingleLine();
	    	}
    	}
		else {
			btnLatest.setVisibility(View.GONE);
			btnHigh.setVisibility(View.GONE);
			btnFriends.setVisibility(View.GONE);
		}
		
		return convertView; 
		 
	} 

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return persons[groupPosition];
	}

	@Override
	public int getGroupCount() {
		return persons.length;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {      
		if (convertView == null) {     
			LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);     
			convertView = inflater.inflate(R.layout.group_row, null);    }     
		
		TextView tvGroupName = (TextView) convertView.findViewById(R.id.row_name);    
		TextView tvGroupScore = (TextView)convertView.findViewById(R.id.row_score);
		TextView tvGroupRank = (TextView)convertView.findViewById(R.id.row_rank);
		Typeface font = Util.customFont(myContext);
		tvGroupName.setTypeface(font);
		tvGroupScore.setTypeface(font);
		tvGroupRank.setTypeface(font);
		
		Person p = persons[groupPosition];
		tvGroupName.setText(p.getName());
		
		tvGroupScore.setText(Util.formatNumber(p.getScore()) + " " + myContext.getString(R.string.points));
		int a = groupPosition + 1;
		tvGroupRank.setText("" + a);
		
		ImageView picView = (ImageView)convertView.findViewById(R.id.row_pic);
		PictureHandler.viewImageFromDevice(myContext, p, Emotion.NORMAL, picView);
		
		return convertView;   
	}   

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

}
